package com.example.myapplication01;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication01.listview.Ultraman;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private final String[] data = {"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple"};


    static class UltramanAdapter extends BaseAdapter {

        private int resourceId;
        private List<Ultraman> list;

        public UltramanAdapter(int resourceId, List<Ultraman> list) {
            this.resourceId = resourceId;
            this.list = list;
        }


        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Ultraman getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(resourceId, parent, false);
                // convertView = View.inflate(parent.getContext(), resourceId, parent);
                // 获取子项视图
                viewHolder.ultramanImage = convertView.findViewById(R.id.ultraman_image);
                viewHolder.ultramanName = convertView.findViewById(R.id.ultraman_name);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            // 设置视图数据
            Ultraman ultraman = getItem(position);
            viewHolder.ultramanImage.setImageResource(ultraman.getImgId());
            viewHolder.ultramanName.setText(ultraman.getName());

            return convertView;
        }

        static class ViewHolder {
            ImageView ultramanImage;
            TextView ultramanName;
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_list_view);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.view_model), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        List<Ultraman> list = new ArrayList<>();
        list.add(new Ultraman(R.mipmap.seven_1, "Seven1"));
        list.add(new Ultraman(R.mipmap.seven_8, "Seven8"));
        list.add(new Ultraman(R.mipmap.seven_9, "Seven9"));
        list.add(new Ultraman(R.mipmap.seven_12, "Seven12"));
        list.add(new Ultraman(R.mipmap.seven_20, "Seven20"));
        list.add(new Ultraman(R.mipmap.seven_34, "Seven34"));
        // ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
        UltramanAdapter ultramanAdapter = new UltramanAdapter(R.layout.ultraman_item, list);

        ListView listView = findViewById(R.id.list_view);
        // listView.setAdapter(adapter);
        listView.setAdapter(ultramanAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            Ultraman ultraman = (Ultraman) ultramanAdapter.getItem(position);
            Toast.makeText(ListViewActivity.this, ultraman.getName(), Toast.LENGTH_SHORT).show();
        });


    }
}